package cn.dglydrpy.study.algorithm.od;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author Yyy
 * @date 2024/11/8 15:22
 * @description
 * 在有限时间内处理多个任务，每个任务有最晚处理时间限制和积分值，求可获得的最多积分。
 * 输入：
4
3
1 2
1 3
1 4
3 5
输出：
9
 */
public class Od0025_TaskScore {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            int n = Integer.valueOf(scanner.nextLine());
            int taskCount = Integer.valueOf(scanner.nextLine());
            int[][] arr = new int[n][2];
            for(int i = 0 ;i < arr.length;i++){
                String[] s = scanner.nextLine().split(" ");
                arr[i][0] = Integer.valueOf(s[0]);
                arr[i][1] = Integer.valueOf(s[1]);
            }

            int score = myAnswer(arr,taskCount);
            System.out.println(score);
        }
    }

    private static int myAnswer(int[][] arr,int taskCount) {
        if(arr == null || arr.length < 1){
            return 0;
        }
        if(taskCount < 1){
            return 0;
        }

        int ans = 0;

        List<Task> tasks = new ArrayList<>();
        for(int i = 0;i<arr.length;i++){
            Task task = new Task(arr[i][0],arr[i][1]);
            tasks.add(task);
        }

        Collections.sort(tasks,
                (o1,o2) -> o1.endTime == o2.endTime ? (o2.score - o1.score) : (o1.endTime - o2.endTime)
        );
        // 3
        // 1  2
        // 1  3
        // 1  4
        // 3  5
        for(int i = 0;i<taskCount;i++){
            // 当前时间点里找max的分数
            int curScore = 0;
            for(Task task : tasks){
                if(task.endTime == i+1){
                    curScore = task.score;
                    break;
                }
            }
            ans += curScore;
        }

        return ans;

    }




    private static class Task{
        private int endTime;
        private int score;

        public Task(int endTime, int score) {
            this.endTime = endTime;
            this.score = score;
        }
    }
}
